package com.ruoyi.customer.service.impl;


import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.customer.domain.CustomerContract;
import com.ruoyi.customer.domain.CustomerContractItem;
import com.ruoyi.customer.dto.AttachmentInfo;
import com.ruoyi.customer.dto.CustomerContractDTO;
import com.ruoyi.customer.mapper.CustomerContractMapper;
import com.ruoyi.customer.service.CustomerContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:zzm
 * @Date: 2024/9/2 10:43
 */
@Service
public class CustomerContractServiceImpl extends ServiceImpl<CustomerContractMapper, CustomerContract> implements CustomerContractService {

    @Autowired
    private CustomerContractMapper customerContractMapper;
    @Autowired
    private CustomerContractItemServiceImpl customerContractItemServiceImpl;

    /**
     * 获取列表
     *
     * @param dto
     * @return
     */
    @Override
    public List<CustomerContract> getList(CustomerContractDTO dto) {
        List<CustomerContract> list = customerContractMapper.getList(dto.getCustomerNickName(),dto.getCustomerId());
        return list;
    }

    /**
     * 新增和修改
     *
     * @param ro
     * @return
     */
    @Override
    public Boolean addOrUpdate(CustomerContract ro) {
        if (ro.getId() != null) {
            this.updateById(ro);
        } else {
            this.save(ro);
        }
        return true;
    }

    /**
     * 删除放入回收站
     *
     * @param ids
     */
    @Override
    public void delete(Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            customerContractMapper.deleteByIds(ids[i]);
        }
    }

    //附件
    @Override
    public List<AttachmentInfo> getAttachmentList(String parentId) {
        if (parentId == null) {
            return Collections.emptyList();
        }
        CustomerContractItem attachment = customerContractMapper.selectByParentIdAndDate(parentId);
        if (attachment == null || StringUtils.isBlank(attachment.getFileList())) {
            return Collections.emptyList();
        }

        try {
            return JSON.parseArray(attachment.getFileList(), AttachmentInfo.class);
        } catch (Exception e) {
            //log.error("解析附件列表失败, parentId: {}, date: {}", parentId, date, e);
            return Collections.emptyList();
        }
    }

    @Override
    public boolean saveOrUpdateAttachments(String parentId, List<AttachmentInfo> attachments) {
        if (parentId == null ) {
            throw new IllegalArgumentException("parentId不能为空");
        }

        // 查询是否已存在记录
        CustomerContractItem existing = customerContractMapper.selectByParentIdAndDate(parentId);

        // 创建或更新实体
        CustomerContractItem entity = new CustomerContractItem();
        entity.setParentId(parentId);
        entity.setFileList(JSON.toJSONString(attachments));

        if (existing != null) {
            entity.setId(existing.getId());
            return customerContractItemServiceImpl.updateById(entity);
        }
        return customerContractItemServiceImpl.save(entity);
    }


    //附件-报价
    @Override
    public List<AttachmentInfo> getAttachmentListPrice(String parentId) {
        if (parentId == null) {
            return Collections.emptyList();
        }
        CustomerContractItem attachment = customerContractMapper.selectByParentIdAndDate(parentId);
        if (attachment == null || StringUtils.isBlank(attachment.getFileListPrice())) {
            return Collections.emptyList();
        }

        try {
            return JSON.parseArray(attachment.getFileListPrice(), AttachmentInfo.class);
        } catch (Exception e) {
            //log.error("解析附件列表失败, parentId: {}, date: {}", parentId, date, e);
            return Collections.emptyList();
        }
    }

    @Override
    public boolean saveOrUpdateAttachmentsPrice(String parentId, List<AttachmentInfo> attachments) {
        if (parentId == null ) {
            throw new IllegalArgumentException("parentId不能为空");
        }

        // 查询是否已存在记录
        CustomerContractItem existing = customerContractMapper.selectByParentIdAndDate(parentId);

        // 创建或更新实体
        CustomerContractItem entity = new CustomerContractItem();
        entity.setParentId(parentId);
        entity.setFileListPrice(JSON.toJSONString(attachments));

        if (existing != null) {
            entity.setId(existing.getId());
            return customerContractItemServiceImpl.updateById(entity);
        }
        return customerContractItemServiceImpl.save(entity);
    }


}
